package com.ikun.yummy.sourceop.op;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ikun.yummy.bean.UserInfo;
import com.ikun.yummy.properties.AppProperties;
import com.ikun.yummy.sourceop.sqlutils.SqliteHelper;
public class UserOP {
    private final SqliteHelper userhelper;
    private final SQLiteDatabase db;
    public UserOP(Context context){
        userhelper = new SqliteHelper(context);
        db = userhelper.getReadableDatabase();
    }
    //注册默认登录了
    public boolean sign(UserInfo userInfo){
        @SuppressLint("Recycle") Cursor cursor= db.query(AppProperties.USER_TABLE, new String[]{"username"},"username=?", new String[]{userInfo.username}, null, null, null, null);
        @SuppressLint("Recycle") Cursor cursor1 = db.query(AppProperties.USER_TABLE, new String[]{"username"},"email=?", new String[]{userInfo.email}, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0 && cursor1 != null && cursor1.getCount()>0) {
          return false;
        }else{
            db.beginTransaction();
            ContentValues values = new ContentValues();
            values.put("username", userInfo.username);
            values.put("password", userInfo.password);
            values.put("userimage", userInfo.userimage);
            values.put("loginstate", userInfo.loginstate);
            values.put("email", userInfo.email);
            db.insert(AppProperties.USER_TABLE, null, values);
            db.setTransactionSuccessful();
            db.endTransaction();
            return true;
        }
    }
    //登录
    @SuppressLint("Recycle")
    public UserInfo login(UserInfo userInfo, String by){
        Cursor cursor=null;
        if (by.equals("username")){
            cursor = db.query(AppProperties.USER_TABLE, new String[]{"username","password","userimage"}, "username=? AND password=?", new String[]{userInfo.username, userInfo.password}, null, null, null, null);
        }
        if (by.equals("email")){
            cursor = db.query(AppProperties.USER_TABLE, new String[]{"username","password","userimage"}, "email=? AND password=?", new String[]{userInfo.username, userInfo.password}, null, null, null, null);
        }
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()){
                //登录就更新用户状态
                updateByUsername(new UserInfo(cursor.getString(0),cursor.getString(1),"1",cursor.getString(2),null));
                return new UserInfo(cursor.getString(0),cursor.getString(1),"1",cursor.getString(2),null);
            }
            return null;
        }else{
           return null;
        }
    }
    public void updateByEmail(UserInfo userInfo){
        SQLiteDatabase db = userhelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        if (userInfo.password!=null){
            values.put("password", userInfo.password);
        }
        if (userInfo.userimage !=null){
            values.put("userimage", userInfo.userimage);
        }
        values.put("loginstate", userInfo.loginstate);
        db.update(AppProperties.USER_TABLE,values, "email=?" ,new String[]{userInfo.email});
    }
    //null为不更新,password和userimage可选
    public void updateByUsername(UserInfo userInfo){
        SQLiteDatabase db = userhelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        if (userInfo.password!=null){
            values.put("password", userInfo.password);
        }
        if (userInfo.userimage!=null){
            values.put("userimage", userInfo.userimage);
        }
        if (userInfo.email!=null){
            values.put("email", userInfo.email);
        }
        values.put("loginstate", userInfo.loginstate);
        db.update(AppProperties.USER_TABLE,values, "username=?" ,new String[]{userInfo.username});
    }
    //通过用户名获取用户信息
    public UserInfo queryUserByUsername(String username){
        @SuppressLint("Recycle") Cursor cursor = db.query(AppProperties.USER_TABLE, new String[]{"username","password","loginstate","userimage","email"}, "username=?", new String[]{username}, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()){
                return new UserInfo(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
            }
            return null;
        }else{
            return null;
        }
    }
    //通过用户名获取用户信息
    public UserInfo queryUserByEmail(String email){
        @SuppressLint("Recycle") Cursor cursor = db.query(AppProperties.USER_TABLE, new String[]{"username","password","loginstate","userimage","email"}, "email=?", new String[]{email}, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()){
                return new UserInfo(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
            }
            return null;
        }else{
            return null;
        }
    }
}